package com.ymatou.shop.ui.view.item;

import java.util.Calendar;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ymatou.shop.R;
import com.ymatou.shop.cache.ImageCache;
import com.ymatou.shop.model.Order;
import com.ymatou.shop.util.DateUtil;

public class BuyerOrderItemView extends FrameLayout {

	private ImageCache.OnIconLoadCompleteListener mOnIconLoadCompleteListener;
	
	private TextView mTimeTextView;
	private TextView mOrderCodeTextView;
	private TextView mDownPaymentTextView;
	private TextView mPriceTextView;
	private TextView mNumTextView;
	private TextView mDescTextView;
	private View mCountDownRootView;
	private TextView mCountDownTextView;
	private ImageView mIconImageView;
	
	private Button mPaymentButton;
	private Button mCancelButton;
	private TextView mStatusTextView;
	
	private Drawable mDefaultDrawable;
	
	private Order mItem;
	
	public BuyerOrderItemView(Context context, ImageCache.OnIconLoadCompleteListener iconListener) {
		super(context);
		mOnIconLoadCompleteListener = iconListener;
		mDefaultDrawable = getResources().getDrawable(R.drawable.ic_launcher);
		
		initOrderItemView();
	}
	
	private void initOrderItemView() {
		LayoutInflater.from(getContext()).inflate(R.layout.list_buyer_order_item, this);
		
		mTimeTextView = (TextView) findViewById(R.id.time_txt);
		mOrderCodeTextView = (TextView) findViewById(R.id.order_code_txt);
		mDownPaymentTextView = (TextView) findViewById(R.id.downpayment_txt);
		mPriceTextView = (TextView) findViewById(R.id.price_txt);
		mNumTextView = (TextView) findViewById(R.id.num_txt);
		mIconImageView = (ImageView) findViewById(R.id.thumb_image);
		mDescTextView = (TextView) findViewById(R.id.description_txt);
		mCountDownRootView = findViewById(R.id.countdown_root);
		mCountDownTextView = (TextView) findViewById(R.id.countdown_txt);
		mPaymentButton = (Button) findViewById(R.id.payment_button);
		mCancelButton = (Button) findViewById(R.id.cancel_button);
		mStatusTextView = (TextView) findViewById(R.id.status_txt);
	}
	
	public void setData(Order order) {
		mItem = order;
		
		setData();
	}
	
	private void setData() {
		mTimeTextView.setText("下单时间：" + DateUtil.format(mItem.mCreateTime, "MM-dd HH:mm:ss"));
		mOrderCodeTextView.setText("订单编号：" + mItem.mId);
		mDownPaymentTextView.setText(String.valueOf(mItem.mDownPayment));
		mPriceTextView.setText(String.valueOf(mItem.mPrice));
		mNumTextView.setText(String.valueOf(mItem.mNum));
		mDescTextView.setText(mItem.mProductDescription);
		
		Drawable drawable = ImageCache.get(getContext(), mItem.mProductMainPic, mOnIconLoadCompleteListener);
		if (drawable == null) {
			mIconImageView.setImageDrawable(mDefaultDrawable);
		} else {
			mIconImageView.setImageDrawable(drawable);
		}
		
		mCountDownRootView.setVisibility(View.GONE);
		mPaymentButton.setVisibility(View.GONE);
		mCancelButton.setVisibility(View.GONE);
		mStatusTextView.setVisibility(View.GONE);
		
		if (mItem.mStatus == Order.STATUS_CREATE) {
			mPaymentButton.setText("付款");
			mPaymentButton.setVisibility(View.VISIBLE);
			processCountDown();
			mCountDownRootView.setVisibility(View.VISIBLE);
		} else if (mItem.mStatus == Order.STATUS_FILL_PRICE) {
			mPaymentButton.setText("补款");
			mPaymentButton.setVisibility(View.VISIBLE);
			processCountDown();
			mCountDownRootView.setVisibility(View.VISIBLE);
		} else if (mItem.mStatus == Order.STATUS_SELLER_ACCESS) {
			mStatusTextView.setText("卖家已接单");
			mStatusTextView.setVisibility(View.VISIBLE);
		} else if (mItem.mStatus == Order.STATUS_PAYMENT) {
			mStatusTextView.setText("等待卖家发货");
			mStatusTextView.setVisibility(View.VISIBLE);
		} else if (mItem.mStatus == Order.STATUS_SEND) {
			mStatusTextView.setText("确认收货");
			mStatusTextView.setVisibility(View.VISIBLE);
		} else if (mItem.mStatus == Order.STATUS_RECEIVE) {
			mStatusTextView.setText("已收货");
			mStatusTextView.setVisibility(View.VISIBLE);
		}
	}

	private void processCountDown() {
		if (mItem.mStatus != Order.STATUS_CREATE
				&& mItem.mStatus != Order.STATUS_FILL_PRICE) {
			return;
		}
		if (mItem.mLastPayTime == null || mItem.mCreateTime == null) {
			mCountDownRootView.setVisibility(View.GONE);
			return;
		}
		
		long time = mItem.mLastPayTime.getTime() - System.currentTimeMillis();
		if (time > 0) {
			StringBuffer sb = new StringBuffer();
			int hour = (int) (time / DateUtil.MILLIS_PER_HOUR);
			if (hour > 0) {
				sb.append(hour > 9 ? hour : "0" + hour).append(":");
			} else {
				sb.append("00:");
			}
			time -= hour * DateUtil.MILLIS_PER_HOUR;
			
			int minute = (int) (time / DateUtil.MILLIS_PER_MINUTE);
			if (minute > 0) {
				sb.append(minute > 9 ? minute : "0" + minute).append(":");
			} else {
				sb.append("00:");
			}
			time -= minute * DateUtil.MILLIS_PER_MINUTE;
			
			int second = (int) (time / DateUtil.MILLIS_PER_SECOND);
			if (second > 0) {
				sb.append(second > 9 ? second : "0" + second);
			} else {
				sb.append("00");
			}
			
			mCountDownTextView.setText(sb.toString());
			mCountDownRootView.setVisibility(View.VISIBLE);
			postDelayed(new Runnable() {
				@Override
				public void run() {
					processCountDown();
				}
			}, 1000);
		} else {
			mCountDownRootView.setVisibility(View.GONE);
		}
	}
}
